package com.topone.controller;


import com.topone.Exception.FanZoneException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.Sysuser;
import com.topone.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/sysUser")
@Slf4j
@CrossOrigin
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public ResultVO login(@RequestParam("password") String password,
                          @RequestParam("userName") String userName) {
        Map<String, Object> map = new HashMap<>();
        try {
            Sysuser sysuser = new Sysuser();
            sysuser.setUserName(userName);
            sysuser.setPassword(password);
            Sysuser Result = sysUserService.login(sysuser);
            if(Objects.nonNull(Result)) {
                map.put("name", Result.getUserName());
                map.put("id", Result.getUserId());
            }else{
                log.debug("用户名或者密码不正确");
                return ResultVOUntil.error(1,"用户名或者密码不正确");
            }
        } catch (FanZoneException e) {
            log.debug("管理员登录失败:{}", e.fillInStackTrace());
            return ResultVOUntil.error(1, "管理员登录失败:" + e.fillInStackTrace());
        }
        log.debug("管理员登录成功", map);
        return ResultVOUntil.success(map);
    }
}
